<?php

/**
 * 日志类
 */
class logutil 
{
	//log名
	private $logFile = "";
	//log级别
	private $logLevel = "";
	//log级别数组
	private $logLevels = array (
					'NONE' => 0,
					'ERROR' => 1,
					'INFO' => 2,
					'DEBUG' => 3
				);
	//log加密区分
	private $logEncrypt = false;
	//加密key
	private $encryptKey = "";
	//解密后log文件名
	private $logFileDecrypt = "";
	
	
	/**
	 * 构造函数
	 * @param $date     日期(YYYYMMDD)      （默认值 = 系统时间）
	 * @param $logEncrypt   log是否加密(true or false)      （默认值 = config.php/LOG_ENCRYPT）
	 * @param $encryptKey   加密key(string)      （默认值 = config.php/ENCRYPT_KEY）
   	 */
	public function __construct($date="",$logFile=LOG_FILE) 
	{
		//log文件名
		$logFile = LOG_FILE;
		//log级别
		$this->logLevel = strtoupper(LOG_LEVEL);
		//log是否加密
		$this->logEncrypt = LOG_ENCRYPT;
		//加密key
		$this->encryptKey = ENCRYPT_KEY;
		//拼出log文件名
		$posi = strrpos($logFile,'.');
		if($posi !== FALSE)
		{
			$path1 = substr($logFile,0,$posi);
			$path2 = substr($logFile,$posi);
			if(is_string($date) && strlen($date)==8)
			{
				$this->logFile = $path1."_".$date.$path2;
			}
			else if($date == "")
			{
				$this->logFile = $path1."_".date("Ymd").$path2;
			}
			else
			{
				$this->logFile = $path1."_".$date.$path2;
			}
		}
		else
		{
			$this->logFile = $logFile;
		}
	}
	
	
	/**
	 * 写debug log
	 * @param $message log
   	 */
	public function debug($message) 
	{
		$level = 'DEBUG';
		//log级别 >= DEBUG时
		if ($this->logLevels[$this->logLevel] >= $this->logLevels[$level]) 
		{
			//log格式化
			$message = date("Y-m-d H:i:s") . "  DEBUG [" . $_SERVER['REQUEST_URI'] . "] [" . $_SERVER['REMOTE_ADDR'] . "]   " . $message;
			//写log
			$this->write_log($message);
		}
	}
	
	/**
	 * 写INFO log
	 * @param $message log
   	 */
	public function info($message) 
	{
		$level = 'INFO';
		//log级别 >= INFO时
		if ($this->logLevels[$this->logLevel] >= $this->logLevels[$level]) 
		{
			//log格式化
			$message = date("Y-m-d H:i:s") . "  INFO [" . $_SERVER['REQUEST_URI'] . "] [" . $_SERVER['REMOTE_ADDR'] . "]   " . $message;
			//写log
			$this->write_log($message);
		}
	}
	
	/**
	 * 写ERROR log
	 * @param $message log
   	 */
	public function error($message) 
	{
		$level = 'ERROR';
		//log级别 >= ERROR时
		if ($this->logLevels[$this->logLevel] >= $this->logLevels[$level]) 
		{
			//log格式化
			$message = date("Y-m-d H:i:s") . "  ERROR [" . $_SERVER['REQUEST_URI'] . "] [" . $_SERVER['REMOTE_ADDR'] . "]   " . $message;
			//写log
			$this->write_log($message);
		}
		//_error('','运行出错');
	}
	
	
	/**
	 * 删除log文件
   	 */
	public function log_reset() 
	{
		@unlink($this->logFile);
	}
	
	/**
	 * 取得log名
	 * @return log名
   	 */
	public function getLogFile() 
	{
		return $this->logFile;
	}
	
	
	/**
	 * 写log
	 * @param $message log
   	 */
	private function write_log($message) 
	{
		//log名为空时
		if (strcmp($this->logFile, "")==0) 
		{
			echo $message;
		}
		//log名不为空时
		else 
		{
			$fp = @fopen($this->logFile, "a+");
			if($fp === false)
			{
			}
			//log需要加密时
			if($this->logEncrypt === true)
			{
				//加密
				$message = $this->passport_encrypt($message,$this->encryptKey);
			}
			if(@fwrite($fp, "$message\n") === false)
			{
			}
			@fclose($fp);
		}
	}
	
	/**
	 * 加密log
	 * @param $message log
	 * @param $key 　　加密key
   	 */
	private function passport_encrypt($txt, $key) 
	{
		srand((double)microtime() * 1000000);
		$encrypt_key = md5(rand(0, 32000));
		$ctr = 0;
		$tmp = '';
		for($i = 0;$i < strlen($txt); $i++) 
		{
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
		}
		return base64_encode($this->passport_key($tmp, $key));
	}
	
	
	private function passport_key($txt, $encrypt_key) 
	{
		$encrypt_key = md5($encrypt_key);
		$ctr = 0;
		$tmp = '';
		for($i = 0; $i < strlen($txt); $i++) 
		{
			$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
			$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
		}
		return $tmp;
	}
	
}
?>